package workbench.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import settings.domain.User;
import util.DateUtil;
import util.JsonUtil;
import util.UUIDUtil;
import workbench.VO.FlagAndObject;
import workbench.VO.ListAndObject;
import workbench.VO.VO;
import workbench.domain.Activity;
import workbench.domain.ActivityRemark;
import workbench.exception.DeleteException;
import workbench.exception.SaveRemarkException;
import workbench.exception.UpdateException;
import workbench.service.ActivityService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @RequestMapping(value = "/selectOwner")
    @ResponseBody
    public String selectOwner() throws JsonProcessingException {
        ArrayList<Activity> activities= (ArrayList<Activity>) activityService.selectOwners();
        ObjectMapper objectMapper=new ObjectMapper();
        StringBuilder sb=new StringBuilder();
        sb.append("{\"dataList\":[");
        for(int i=0;i<activities.size()-1;i++){
            sb.append(objectMapper.writeValueAsString(activities.get(i))+",");
        }
        sb.append(objectMapper.writeValueAsString(activities.get(activities.size()-1)));
        sb.append("]}");
        return sb.toString();
    }
    @RequestMapping(value = "/addActivity",method = RequestMethod.POST)
    @ResponseBody
    public String selectOwner(Activity activity, HttpServletRequest req){
        HttpSession session=req.getSession();
        User user= (User) session.getAttribute("userMessage");
        activity.setId(UUIDUtil.getUUID());
        activity.setCreateBy(user.getName());
        activity.setEditBy(user.getName());
        activity.setEditTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
        activity.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
        boolean success=activityService.addActivity(activity);
        if(success){
            return JsonUtil.ordinaryValueToJson("success","true");
        }else {
            return JsonUtil.ordinaryValueToJson("success","false");
        }
    }
    @RequestMapping(value = "/getActivityList")
    @ResponseBody
    public String getActivityList(HttpServletRequest req,Activity activity) throws JsonProcessingException {
        int pageNum=Integer.parseInt(req.getParameter("pageNum"));
        int pageSize=Integer.parseInt(req.getParameter("pageSize"));
        List<Activity> activityList=activityService.getActivityList(pageNum,pageSize,activity);
        int countActivity=activityService.countActivity(activity);
        ObjectMapper objectMapper=new ObjectMapper();
        VO<Activity> vo=new VO<>(countActivity,activityList);
        String s=objectMapper.writeValueAsString(vo);
        return s;
    }
    @RequestMapping(value = "/deleteActivity",method = RequestMethod.POST)
    @ResponseBody
    public String deleteActivity(HttpServletRequest req) throws DeleteException {
        String[] ids=req.getParameterValues("id");
        if(activityService.deleteActivity(ids)){
            return JsonUtil.ordinaryValueToJson("success","true");
        }else {
            return JsonUtil.ordinaryValueToJson("success","false");
        }
    }
    @RequestMapping(value = "/getUserListAndActivity")
    @ResponseBody
    public String getUserListAndActivity(HttpServletRequest req) throws DeleteException, JsonProcessingException {
        String id=req.getParameter("id");
        ListAndObject<Activity> userListAndActivity=activityService.getUserListAndActivity(id);
        String json=JsonUtil.writeValueAsString(userListAndActivity);
        return json;
    }
    @RequestMapping(value = "/updataActivity",method = RequestMethod.POST)
    @ResponseBody
    public String updataActivity(HttpServletRequest req,Activity activity) throws UpdateException {
        activityService.updataActivity(req,activity);
        return JsonUtil.ordinaryValueToJson("success","true");
    }
    @RequestMapping(value = "/detail")
    public ModelAndView detail(HttpServletRequest req){
        req.setAttribute("activityDetail",activityService.detail(req.getParameter("id")));
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("detail");
        return modelAndView;
    }
    @RequestMapping(value = "/getRemarkMessage")
    @ResponseBody
    public String getRemarkMessage(HttpServletRequest req) throws JsonProcessingException {
        String id=req.getParameter("id");
        List<ActivityRemark> list=activityService.getRemarkMessageById(id);
        String json=JsonUtil.writeValueAsString(list);
        return json;
    }
    @RequestMapping(value = "/saveRemark")
    @ResponseBody
    public String saveRemark(HttpServletRequest req,ActivityRemark activityRemark) throws SaveRemarkException, JsonProcessingException {
        activityRemark.setId(UUIDUtil.getUUID());
        activityRemark.setCreateBy(((User) req.getSession().getAttribute("userMessage")).getName());
        activityRemark.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
        activityRemark.setEditFlag("0");
        activityService.saveRemark(activityRemark);
        String json=JsonUtil.writeValueAsString(new FlagAndObject<>(activityRemark,true));
        return json;
    }
}
